//
//  HNSH_WithdrawalViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/14.
//

#import "HNSH_WithdrawalViewController.h"

#import "HNSH_WithdrawalCellView.h"
#import "ST_PayPasswordView.h"

#import "HNSH_WithdrawalRecordViewController.h"
#import "HNSH_AddWithdrawalAccountViewController.h"

@interface HNSH_WithdrawalViewController ()

/* bgView */
@property(nonatomic, strong) UIView *bgView;

/* submitButton */
@property(nonatomic, strong) UIButton *submitButton;
/* allButton */
@property (nonatomic, strong) UIButton *allButton;

/* 提现金额 */
@property(nonatomic, strong) UILabel *titleLabel1;
/* ¥ */
@property (nonatomic, strong) UILabel *yangLabel;
/* 提现金额 */
@property(nonatomic, strong) UITextField *textField;
/* 提现方式 */
@property(nonatomic, strong) UILabel *titleLabel2;
/* 可提现余额 */
@property(nonatomic, strong) UILabel *ktxLabel;
/* des */
@property(nonatomic, strong) UILabel *desLabel;

/* payItemAry */
@property(nonatomic, strong) NSMutableArray *payItemAry;

/* selectType */
@property (nonatomic, assign) NSInteger selectType;

/* configModel */
@property (nonatomic, strong) HNSH_WithdrawalModel *configModel;
@end

@implementation HNSH_WithdrawalViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadWithdrawalInfoData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"提现申请" showLeftItem:YES];
    
    __weak __typeof(&*self)weakSelf = self;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth-95, kStatusBarHeight, 80, 44);
    [button setTitle:@"提现记录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.navView addSubview:button];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_WithdrawalRecordViewController *vc = [[HNSH_WithdrawalRecordViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [self addAllViews];
}

#pragma mark event
- (void)cellViewButtonClick:(UIButton *)sender {
    for (HNSH_WithdrawalCellView *cellView in self.payItemAry) {
        cellView.selectButton.selected = NO;
    }
    sender.selected = YES;
    self.selectType = sender.tag;
}

//MARK: 支付
- (void)pay{
    WeakSelf(weakSelf);
    [ST_PayPasswordView showPasswordViewWithSureBlock:^(NSString * _Nonnull password) {
        [weakSelf loadWithdrawalWithPwd:password];
    } cancelBlock:^{
        
    }];
}

- (void)addAllViews {
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.submitButton];
    
    [self.bgView addSubview:self.titleLabel1];
    [self.bgView addSubview:self.yangLabel];
    [self.bgView addSubview:self.textField];
    __weak __typeof(&*self)weakSelf = self;
    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.yangLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    }];
    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        if (field.text.length <= 0) {
            weakSelf.yangLabel.textColor = [UIColor colorWithHexString:@"#D3D3D3"];
        }
        if (weakSelf.selectType == 0 || [field.text doubleValue] <= 0) {
            weakSelf.submitButton.enabled = NO;
            weakSelf.submitButton.backgroundColor = [UIColor appGrayLabelColor];
        } else {
            weakSelf.submitButton.enabled = YES;
            weakSelf.submitButton.backgroundColor = [UIColor appGreenColor];
        }
    }];
    [self.bgView addSubview:self.allButton];
    [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.equalTo(self.textField.mas_centerY);
        make.width.mas_equalTo(76);
        make.height.mas_equalTo(self.textField.height);
    }];
    
    CALayer *line = [[CALayer alloc]init];
    line.frame = CGRectMake(24, 99, kScreenWidth-48, 1);
    line.backgroundColor = [[UIColor colorWithHexString:@"#EDEDED"]CGColor];
    [self.bgView.layer addSublayer:line];
    
    [self.bgView addSubview:self.ktxLabel];
    self.ktxLabel.frame = CGRectMake(24, line.origin.y+1+7, kScreenWidth-48, 17);
    [self.bgView addSubview:self.desLabel];
    [self.desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.top.mas_equalTo(self.ktxLabel.origin.y+17+7);
    }];
    
    [self.bgView addSubview:self.titleLabel2];
    [self.titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.top.equalTo(self.desLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(28);
    }];
}


- (void)configCellViewWithModel:(HNSH_WithdrawalModel *)model {
    __weak typeof(self) weakSelf = self;
    UIView *priView = self.titleLabel2;
    for (int i = 0; i < model.accounts.count; i ++) {
        HNSH_AccountsItem *item = model.accounts[i];
        HNSH_WithdrawalCellView *cellView = [[HNSH_WithdrawalCellView alloc]initWithFrame:CGRectMake(0, 176+56*i, kScreenWidth, 56)];
        [self.bgView addSubview:cellView];
        [cellView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(56);
            make.top.mas_equalTo(priView.mas_bottom);
        }];
        priView = cellView;
        [cellView.icon sd_setImageWithURL:[NSURL URLWithString:item.icon]];
        if (item.type == 1) {
            cellView.titleLabel.text = @"支付宝";
            cellView.icon.image = [UIImage imageNamed:@"tz_tx_icon_alipay"];
        } else if (item.type == 2) {
            cellView.titleLabel.text = @"银行卡";
            cellView.icon.image = [UIImage imageNamed:@"tz_tx_icon_bank"];
        }
        cellView.desLabel.text = item.status == 0 ? @"未绑定" : item.name;
        cellView.selectButton.hidden = YES;
        if (i == 0) {
            cellView.selectButton.selected = YES;
        }
        cellView.selectButton.tag = item.type;
        [cellView.selectButton addTarget:self action:@selector(cellViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.payItemAry addObject:cellView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
//            if (item.status == 0) {
                HNSH_AddWithdrawalAccountViewController *vc = [[HNSH_AddWithdrawalAccountViewController alloc]init];
                vc.type = item.type;
                vc.infoModel = item;
                [weakSelf.navigationController pushViewController:vc animated:YES];
//            }
        }];
        cellView.userInteractionEnabled = YES;
        [cellView addGestureRecognizer:tap];
        if (item.status == 1) {
            cellView.selectButton.hidden = NO;
            cellView.desLabel.text = item.account;
            cellView.showIcon.hidden = YES;
        } else {
            cellView.selectButton.selected = NO;
            cellView.desLabel.text = @"未绑定";
            cellView.selectButton.hidden = YES;
            cellView.showIcon.hidden = NO;
        }
        
        //默认选中
        if (item.status == 1 && self.selectType == 0) {
            self.selectType = item.type;
            cellView.selectButton.selected = YES;
        }
    }
    
    if (self.selectType == 0 || [self.textField.text doubleValue] <= 0) {
        self.submitButton.enabled = NO;
        self.submitButton.backgroundColor = [UIColor appGrayLabelColor];
//        [weakSelf.view configGradientWithView:weakSelf.submitButton startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5) startColor:[UIColor colorWithHexString:@"#999999"] endColor:[UIColor colorWithHexString:@"#999999"]];
    } else {
        self.submitButton.enabled = YES;
        self.submitButton.backgroundColor = [UIColor appGreenColor];
//        [weakSelf.view configGradientWithView:weakSelf.submitButton startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5) startColor:[UIColor colorWithHexString:@"#FC814C"] endColor:[UIColor colorWithHexString:@"#F73525"]];
    }
}

#pragma mark networking
- (void)loadWithdrawalInfoData{
    [HNSH_UserNetServer requestUserWithdrawConfigSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_WithdrawalModel * model = [HNSH_WithdrawalModel yy_modelWithDictionary:responseObject.resultset];
            self.configModel = model;
            self.payItemAry = [NSMutableArray arrayWithCapacity:model.accounts.count];
            self.selectType = 0;//默认未选择
            self.ktxLabel.text = [NSString stringWithFormat:@"可提现余额: %@",model.balance];
            self.desLabel.text = model.tip_text;
            [self configCellViewWithModel:model];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadWithdrawalWithPwd:(NSString *)pwd {
    [HNSH_UserNetServer requestUserWithdrawApplyWithMoney:self.textField.text account_type:self.selectType pay_password:pwd success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"提现成功"];
            self.textField.text = @"";
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight)];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIButton *)submitButton {
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitButton.frame = CGRectMake(16, kNavBarAndStatusBarHeight+390, kScreenWidth-32, 50);
        [_submitButton setTitle:@"去提现" forState:UIControlStateNormal];
        [_submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _submitButton.layer.cornerRadius = 25;
        _submitButton.layer.masksToBounds = YES;
        _submitButton.backgroundColor = [UIColor appGreenColor];
        __weak __typeof(&*self)weakSelf = self;
        [[_submitButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (![weakSelf.textField.text isNotBlank]) {
                [weakSelf.view makeToast:@"请输入金额"];
                return;
            }
            [weakSelf pay];
        }];
    }
    return _submitButton;
}

- (UIButton *)allButton {
    if (!_allButton) {
        _allButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_allButton setTitle:@"全部" forState:UIControlStateNormal];
        [_allButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _allButton.titleLabel.font = [UIFont systemFontOfSize:14];
        __weak __typeof(&*self)weakSelf = self;
        [[_allButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            weakSelf.textField.text = weakSelf.configModel.balance;
            weakSelf.submitButton.enabled = YES;
            weakSelf.submitButton.backgroundColor = [UIColor appGreenColor];
        }];
    }
    return _allButton;
}

- (UILabel *)titleLabel1 {
    if (!_titleLabel1) {
        _titleLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(24, 20, kScreenWidth-48, 20)];
        _titleLabel1.text = @"提现金额";
        _titleLabel1.textColor = [UIColor colorWithHexString:@"#666666"];
        _titleLabel1.font = [UIFont systemFontOfSize:14];
    }
    return _titleLabel1;
}

- (UILabel *)yangLabel {
    if (!_yangLabel) {
        _yangLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 52, 20, 40)];
        _yangLabel.text = @"¥";
        _yangLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _yangLabel.font = [UIFont boldSystemFontOfSize:28];
    }
    return _yangLabel;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(44, 52, kScreenWidth-68, 40)];
        _textField.textColor = [UIColor colorWithHexString:@"#333333"];
        _textField.font = [UIFont boldSystemFontOfSize:28];
        NSMutableDictionary * attDic = [@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#D3D3D3"], NSFontAttributeName:_textField.font} mutableCopy];
        NSMutableAttributedString * attPlace = [[NSMutableAttributedString alloc] initWithString:@"0.00" attributes:attDic];
        _textField.attributedPlaceholder = attPlace;
        _textField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _textField;
}

- (UILabel *)titleLabel2 {
    if (!_titleLabel2) {
        _titleLabel2 = [[UILabel alloc]init];
        _titleLabel2.text = @"提现方式";
        _titleLabel2.textColor = [UIColor colorWithHexString:@"#666666"];
        _titleLabel2.font = [UIFont systemFontOfSize:14];
    }
    return _titleLabel2;
}

- (UILabel *)ktxLabel {
    if (!_ktxLabel) {
        _ktxLabel = [[UILabel alloc]init];
        _ktxLabel.text = @"可提现余额: 0";
        _ktxLabel.textColor = [UIColor appGrayLabelColor];
        _ktxLabel.font = [UIFont systemFontOfSize:14];
    }
    return _ktxLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.text = @"提现金额需大于0元，单日最多提现0次";
        _desLabel.textColor = [UIColor colorWithHexString:@"#FB6F1C"];
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.numberOfLines = 0;
    }
    return _desLabel;
}

@end
